home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 483 / mkrscsrc / prep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-26  |  10.1 KB  |  410 lines

  1. #include "stdio.h"
  2. #include "gemdefs.h"
  3. #include "obdefs.h"
  4. #include "osbind.h"
  5. #include "globals.h"
  6. #include "strings.h"
  7. #include <fcntl.h>
  8. #include "mkrsc.h"
  9.  
  10.    /*   RSHDR structure             */
  11.             /* rsh_vrsn    */
  12.             /* rsh_object    */
  13.              /* rsh_tedinfo    */
  14.             /* rsh_iconblk    */
  15.             /* rsh_bitblk    */
  16.             /* rsh_frstr    */
  17.             /* rsh_string    string data        */
  18.             /* rsh_imdata    image data        */
  19.             /* rsh_frimg    */
  20.             /* rsh_trindex    */
  21.             /* rsh_nobs    */
  22.             /* rsh_ntree    */
  23.             /* rsh_nted    */
  24.             /* rsh_nib    */
  25.             /* rsh_nbb    */
  26.             /* rsh_nstring    */
  27.             /* rsh_nimages    */
  28.             /* rsh_rssize    */
  29.  
  30. int  fill_rsc_struct();
  31. char *copy_trindex();
  32. char *copy_obs();
  33. char *copy_strings();
  34. long calc_size();
  35. char *copy_tis();
  36.  
  37. char *rel_strptr[MAXONUM];
  38. char *rel_tiptr[MAXONUM];
  39. char *rel_icptr[MAXONUM];
  40.  
  41. /* these rel_xxxptr variables are the relative offset from beginning
  42.     of file image space of the specified things
  43. */ 
  44.  
  45.  
  46. int make_rsc()
  47. {
  48.     long size, lret, rs_trindex[100];
  49.     int handle, check;
  50.     char *start, *next;
  51.     RSHDR *rsc_struct_ptr;    
  52.       
  53. /*
  54.   This programs builds the .RSC file in memory by mallocing space,
  55.   setting the pointer, start, to the space, and then filling the 
  56.   space with the various parts of the file.  Finally the whole image
  57.   is saved to disk.
  58. */
  59.     size = 0L;
  60.  
  61. /*    calc size of entire .RSC file  */
  62.      size = calc_size();
  63.     if(size%2)
  64.         size++;
  65.  
  66. /*    malloc space for file image */
  67.     start = (char *)malloc((unsigned)size); 
  68.  
  69.     bzero(start,(int)size);
  70.  
  71.     rsc_struct_ptr = (RSHDR *)start;
  72.  
  73. /* save space for the RSHDR  structure */
  74.     next = start + sizeof(RSHDR);
  75.  
  76. /* rel offset for string space */
  77.     rsc_struct_ptr->rsh_string = next - start;
  78.  
  79. /* copy strings in */    
  80.     next = copy_strings(next,start,rsc_struct_ptr);
  81.  
  82.     if((int)(next)%2)
  83.         next += 1;
  84.  
  85. /*    next now points to the end of the string space
  86.     copy in the tedinfos
  87. */
  88.     rsc_struct_ptr->rsh_tedinfo = next - start;
  89.     next = copy_tis(next, start,rsc_struct_ptr);
  90.  
  91. /*
  92.     Just fill the rest of these with the string space address
  93.     as they won't be used anyway with just a menu resource 
  94.     and simple dialogs. 
  95. */
  96.  
  97.     rsc_struct_ptr->rsh_iconblk = next - start;
  98.     rsc_struct_ptr->rsh_bitblk = next - start;
  99.     rsc_struct_ptr->rsh_frstr = next - start;
  100.     rsc_struct_ptr->rsh_imdata = next - start;
  101.     rsc_struct_ptr->rsh_frimg = next - start;
  102.  
  103.     rsc_struct_ptr->rsh_object = next - start;
  104.  
  105. /*    copy in the object tree array    */
  106.  
  107.     next = copy_obs(next,start,rsc_struct_ptr,rs_trindex);
  108.  
  109.     rsc_struct_ptr->rsh_trindex = next - start;
  110.     next = copy_trindex(next,rs_trindex);
  111.  
  112.     check = next - start;
  113.  
  114.     rsc_struct_ptr->rsh_rssize = size;
  115.     rsc_struct_ptr->rsh_vrsn = 0;
  116.  
  117.     fill_rsc_struct(rsc_struct_ptr);
  118.  
  119.     fix_ptrs(rsc_struct_ptr,start);
  120.  
  121.     save_file(size,start);
  122.     thefrontwin->saved = TRUE;
  123. }
  124.  
  125. long calc_size()
  126. {
  127.     objtreeptr    thetree, *linkptr;
  128.     OBJECT        *inwinptr, *big_treeptr;
  129.     long size;
  130.     int i,j;
  131.     char *next;
  132.     int numobjs, order[MAXONUM];
  133.  
  134.     thetree = thefrontwin->inwindow;
  135.     linkptr = thetree->treelink;
  136.  
  137.     size = sizeof(RSHDR);
  138.     for( i=1;i < thetree->count + 1; i++)
  139.     {    numobjs = trav_tree(linkptr[i]->objt,order);
  140.         for(j=0;j < numobjs; j++)
  141.         {    switch (linkptr[i]->objt[order[j]].ob_type)
  142.             {
  143.                 case G_TEXT        :
  144.                 case G_BOXTEXT    :
  145.                 case G_FTEXT    :
  146.                 case G_FBOXTEXT    : 
  147.                     size += 1 + strlen(linkptr[i]->strings[order[j]]);
  148.                     size += 1 + strlen(linkptr[i]->template[order[j]]);
  149.                     size += 1 + strlen(linkptr[i]->valid[order[j]]);
  150.                     size += sizeof(TEDINFO);
  151.                     break;
  152.                 case G_BUTTON    :
  153.                 case G_STRING    :
  154.                 case G_TITLE    :
  155.                     size += 1 + strlen(linkptr[i]->strings[order[j]]);
  156.                     break;
  157.                 case G_ICON        :
  158.                     size += 1 + strlen(linkptr[i]->strings[order[j]]);
  159.                     size += sizeof(ICONBLK);
  160.                     break;
  161.             }        
  162.             size += sizeof(OBJECT);
  163.         }
  164.     }
  165.  
  166. /*    make room for tr_index array    */
  167.  
  168.     size += thetree->count * sizeof(long);
  169.     
  170.     return(size);
  171. }
  172.  
  173. char *copy_strings(next, start,rsc_struct_ptr)
  174.     char *next, *start;
  175.     RSHDR    *rsc_struct_ptr;
  176. {
  177.     char *ptr;
  178.     objtreeptr    thetree, *linkptr;
  179.     TEDINFO *tiptr;
  180.     ICONBLK *icptr;
  181.     int i,j,num_str;
  182.     int numobjs, order[MAXONUM];
  183.  
  184.     thetree = thefrontwin->inwindow;
  185.     linkptr = thetree->treelink;
  186.  
  187.     num_str = 0;
  188.     ptr = next;
  189.     for( i=1;i < thetree->count + 1; i++)
  190.     {    numobjs = trav_tree(linkptr[i]->objt,order);
  191.         for(j=0;j < numobjs; j++)
  192.         {    switch (linkptr[i]->objt[order[j]].ob_type)
  193.             {
  194.                 case G_TEXT        :
  195.                 case G_BOXTEXT    :
  196.                 case G_FTEXT    :
  197.                 case G_FBOXTEXT    : 
  198.                     tiptr = (TEDINFO *)linkptr[i]->objt[order[j]].ob_spec;
  199.                     rel_strptr[num_str++] = (char *)(ptr - start);
  200.                     ptr = xtrcpy(ptr,linkptr[i]->strings[order[j]]) + 1;
  201.                     rel_strptr[num_str++] = (char *)(ptr - start);
  202.                     ptr = xtrcpy(ptr,linkptr[i]->template[order[j]]) + 1;
  203.                     rel_strptr[num_str++] = (char *)(ptr - start);
  204.                     ptr = xtrcpy(ptr,linkptr[i]->valid[order[j]]) + 1;
  205.                     break;
  206.                 case G_BUTTON    :
  207.                 case G_STRING    :
  208.                 case G_TITLE    :
  209.                     rel_strptr[num_str++] = (char *)(ptr - start);
  210.                     ptr = xtrcpy(ptr,linkptr[i]->strings[order[j]]) + 1;
  211.                     break;
  212.                 case G_ICON        :
  213.                     icptr = (ICONBLK *)linkptr[i]->objt[order[j]].ob_spec;
  214.                     rel_strptr[num_str++] = (char *)(ptr - start);
  215.                     ptr = xtrcpy(ptr,linkptr[i]->strings[order[j]]) + 1;
  216.                     break;
  217.             }        
  218.         }
  219.     }
  220. /*    rsc_struct_ptr->rsh_nstring = num_str;    */
  221.  
  222. /*    putting the number of strings into rsh_nstring crashes any
  223.     program using the resource!!!  I have no idea why.
  224. */
  225.  
  226.     return(ptr);
  227. }
  228.  
  229. char *copy_tis(next, start,rsc_struct_ptr)
  230.     char *next, *start;
  231.     RSHDR    *rsc_struct_ptr;
  232. {
  233.     char *ptr;
  234.     objtreeptr    thetree, *linkptr;
  235.     TEDINFO *tiptr;
  236.     int i,j, size_ti, num_ti;
  237.     int numobjs, order[MAXONUM];
  238.  
  239.     thetree = thefrontwin->inwindow;
  240.     linkptr = thetree->treelink;
  241.     size_ti = sizeof(TEDINFO);
  242.  
  243.     num_ti = 0;
  244.     ptr = next;
  245.     for( i=1;i < thetree->count + 1; i++)
  246.     {    numobjs = trav_tree(linkptr[i]->objt,order);
  247.         for(j=0;j < numobjs; j++)
  248.         {    switch (linkptr[i]->objt[order[j]].ob_type)
  249.             {
  250.                 case G_TEXT        :
  251.                 case G_BOXTEXT    :
  252.                 case G_FTEXT    :
  253.                 case G_FBOXTEXT    : 
  254.                     tiptr = (TEDINFO *)linkptr[i]->objt[order[j]].ob_spec;
  255.                     bcopy((char *)tiptr,ptr,size_ti);
  256.                     rel_tiptr[num_ti++] = (char *)(ptr - start);
  257.                     ptr += size_ti;
  258.                     break;
  259.             }        
  260.         }
  261.     }
  262.     rsc_struct_ptr->rsh_nted = num_ti;
  263.     return(ptr);
  264. }
  265.  
  266. char *copy_obs(next,start,rsc_struct_ptr,rs_trindex)
  267.     char *next, *start;
  268.     RSHDR    *rsc_struct_ptr;
  269.     long    rs_trindex[];
  270. {
  271.     char *ptr;
  272.     objtreeptr    thetree, *linkptr;
  273.     OBJECT tempobj, *objptr;
  274.     int i,j, size_ob, num_obs, upb;
  275.     int k, ttls[80], head, nx, count;    
  276.     int numobjs, order[MAXONUM];
  277.  
  278.     thetree = thefrontwin->inwindow;
  279.     linkptr = thetree->treelink;
  280.     size_ob = sizeof(OBJECT);
  281.  
  282.     num_obs = 0;
  283.     ptr = next;
  284.     for( i=1;i < thetree->count + 1; i++)
  285.     {    rs_trindex[i-1] = (long)(ptr - start);
  286.         {    numobjs = trav_tree(linkptr[i]->objt,order);
  287.             next = ptr;
  288.             for(j=0;j < numobjs; j++)
  289.             {    tempobj = linkptr[i]->objt[order[j]];
  290.                 /*    fix the size and width and special bytes    */
  291.                 if(order[j]==0)
  292.                     tempobj.ob_x = tempobj.ob_y = 0;
  293.                 upb = (tempobj.ob_x & ~0xFFF8) << 8;
  294.                 tempobj.ob_x = tempobj.ob_x/gl_wchar + upb;
  295.                 upb = (tempobj.ob_width & ~0xFFF8) << 8;
  296.                 tempobj.ob_width = tempobj.ob_width/gl_wchar + upb;
  297.                 upb = (tempobj.ob_y & ~(0xFFFF-gl_hchar+1)) << 8;
  298.                 tempobj.ob_y = tempobj.ob_y/gl_hchar + upb;
  299.                 upb = (tempobj.ob_height & ~(0xFFFF-gl_hchar+1)) << 8;
  300.                 tempobj.ob_height = tempobj.ob_height/gl_hchar + upb;
  301.  
  302. /*    certain objects in menus have the upper byte of ob_height
  303.         or ob_y set to 0x2 or 0x3 to give bit offsets to objects.
  304.  
  305.                 if(thetree->kind[i] == TMENU)
  306.                 {
  307.                     if(order[j]==1)
  308.                         tempobj.ob_height |= 0x200;
  309.                     else if( (order[j]==2)||(tempobj.ob_type == G_TITLE) )
  310.                         tempobj.ob_height |= 0x300;
  311.                     else if(tempobj.ob_next == 0)
  312.                         tempobj.ob_y |= 0x300;
  313.                 }
  314. */
  315.  
  316.     /*    mark last object in subtree    */
  317.                 if(j == (numobjs - 1))
  318.                     tempobj.ob_flags |= LASTOB;
  319.                 else
  320.                     tempobj.ob_flags &= ~LASTOB;
  321.                 bcopy((char *)&tempobj,ptr,size_ob);
  322.                 ptr += size_ob;
  323.                 num_obs++;
  324.             }    /*    end of for(j=....     */
  325. /*    reset the pointers in the object tree...  newpos() in tree.c    */
  326.             objptr = (OBJECT *)next;
  327.             for (j=0;j<numobjs;j++)
  328.             {    objptr[j].ob_next = newpos(objptr[j].ob_next,numobjs,order);
  329.                 objptr[j].ob_head = newpos(objptr[j].ob_head,numobjs,order);
  330.                 objptr[j].ob_tail = newpos(objptr[j].ob_tail,numobjs,order);
  331.             }
  332.  
  333.         }
  334.     }
  335.     rsc_struct_ptr->rsh_nobs = num_obs;
  336.     return(ptr);
  337. }
  338.  
  339. char *copy_trindex(next,rs_trindex)
  340.     char *next;
  341.     long    rs_trindex[];
  342. {
  343.     objtreeptr    thetree, *linkptr;
  344.     int cnt;
  345.  
  346.     thetree = thefrontwin->inwindow;
  347.     linkptr = thetree->treelink;
  348.  
  349.     cnt = thetree->count * (sizeof(long));
  350.     bcopy( (char *)rs_trindex, next, cnt);
  351.     return(next + cnt);
  352. }
  353.  
  354. int  fill_rsc_struct(rsc_struct_ptr)
  355.     RSHDR    *rsc_struct_ptr;
  356.     objtreeptr    thetree;
  357.  
  358.     thetree = thefrontwin->inwindow;
  359.  
  360.     rsc_struct_ptr->rsh_ntree = thetree->count;
  361.     rsc_struct_ptr->rsh_nib = 0;
  362.     rsc_struct_ptr->rsh_nbb = 0;
  363.     rsc_struct_ptr->rsh_nimages = 0;
  364. }
  365.  
  366. int fix_ptrs(rsc_struct_ptr,start)
  367.     RSHDR    *rsc_struct_ptr;
  368.     char     *start;
  369. {
  370.     OBJECT *objptr;
  371.     TEDINFO *tiptr;
  372.     ICONBLK *icptr;
  373.     int i,j, num_str, num_ti, num_ic;
  374.  
  375.  
  376. /* objptr is the start of the array of objects in the file image
  377.     area that is going to be saved in the RSC file.
  378. */
  379.  
  380.     objptr = (OBJECT *)(start + rsc_struct_ptr->rsh_object);
  381.     num_ti = num_str = num_ic = 0;
  382.     
  383.     for( i=0;i < rsc_struct_ptr->rsh_nobs+1; i++)
  384.     {    switch (objptr[i].ob_type)
  385.         {
  386.             case G_TEXT        :
  387.             case G_BOXTEXT    :
  388.             case G_FTEXT    :
  389.             case G_FBOXTEXT    :
  390.                 objptr[i].ob_spec = rel_tiptr[num_ti++];
  391.             tiptr = (TEDINFO *)((long)objptr[i].ob_spec + (long)start);
  392.                 tiptr->te_ptext = rel_strptr[num_str++];
  393.                 tiptr->te_ptmplt = rel_strptr[num_str++];
  394.                 tiptr->te_pvalid = rel_strptr[num_str++];                
  395.                 break;
  396.             case G_BUTTON    :
  397.             case G_STRING    :
  398.             case G_TITLE    :
  399.                     objptr[i].ob_spec = rel_strptr[num_str++];
  400.                     break;
  401.             case G_ICON        :
  402.                     objptr[i].ob_spec = rel_icptr[num_ic++];
  403.                 icptr = (ICONBLK *)((long)objptr[i].ob_spec + (long)start);
  404.                     icptr->ib_ptext = rel_strptr[num_str++];
  405.                     break;
  406.         }        
  407.     }
  408. }
  409.